package com.mdwz.sunnyradio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity implements View.OnClickListener {
	
	public static String tag = "MainActivity";
	
	private Button btHistory;
	private Button btRadio;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btHistory = (Button)this.findViewById(R.id.history);
		btRadio = (Button)this.findViewById(R.id.radio);
		
		btHistory.setOnClickListener(this);
		btRadio.setOnClickListener(this);
		
		this.findViewById(R.id.screenshot).setOnClickListener(this);
		
		Log.i(tag,"Session.openActiveSession");
		
		// start Facebook Login
        Session.openActiveSession(this, true, new Session.StatusCallback() {
        	
            // callback when session changes state
            @Override
            public void call(Session session, SessionState state, Exception exception) {
            	
            	Log.i(tag,"call(Session session, SessionState state, Exception exception)");

                if (session.isOpened()) {
                	
                	Log.i(tag,"session.isOpened()");

                    // make request to the /me API
                    Request.newMeRequest(session, new Request.GraphUserCallback() {

                        // callback after Graph API response with user object
                        @Override
                        public void onCompleted(GraphUser user, Response response) {
                        	
                        	Log.i(tag,"onCompleted(GraphUser user, Response response)");
                        	
                            if (user != null) {
                            	Toast.makeText(getApplicationContext(), "Facebook ID:"+user.getId(), Toast.LENGTH_LONG).show();
                            }
                        }
                    }).executeAsync();
                }
            }
        });
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch ( v.getId() ){
		case R.id.history:{
			Intent intent = new Intent();
			intent.setClass( getApplicationContext() , History.class );
            startActivity( intent );
			break;
		}
			
		case R.id.radio:{
			Intent intent = new Intent();
			intent.setClass( getApplicationContext() , Radio2.class );
            startActivity( intent );
            break;
		}
		
		case R.id.screenshot:{
			
			String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "screenshot.jpg";   

			// create bitmap screen capture
			Bitmap bitmap;
			
			View v1 = getWindow().getDecorView().getRootView();
			v1.setDrawingCacheEnabled(true);
			bitmap = Bitmap.createBitmap(v1.getDrawingCache());
			v1.setDrawingCacheEnabled(false);

			OutputStream fout = null;
			File imageFile = new File(mPath);

			try {
			    fout = new FileOutputStream(imageFile);
			    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
			    fout.flush();
			    fout.close();

			} catch (FileNotFoundException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
			} catch (IOException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
			}
			
			break;
			
		}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
